﻿using UnityEngine;
using System.Collections;

public class Graphic {


	public static GameObject Draw (string Name, Vector2 Size, string Path) {
		GameObject G = new GameObject (Name);
		G.AddComponent <MeshRenderer> ();
		MeshFilter MF = G.AddComponent <MeshFilter> () as MeshFilter;
		MF.mesh = DrawPlane (Size, new Vector2 (0, 0), new Vector2 (1, 1));
		Material M = new Material (Shader.Find ("Mobile/Particles/Alpha Blended"));
		M.mainTexture = Resources.Load (Path) as Texture;
		G.renderer.material = M;
		return G;
	}

	public static Mesh DrawPlane (Vector2 Size, Vector2 Uv, Vector2 UvLength)
	{
		Vector3[] vertices = new Vector3[4];
		Vector3[] normals = new Vector3[4];
		Vector4[] tangents = new Vector4[4];
		Vector2[] uvs = new Vector2[4];
		int[] triangles = new int[6];
		
		
		vertices[1] = new Vector3(-Size.x/2, -Size.y/2, 0.0f);
		vertices[0] = new Vector3(Size.x/2, -Size.y/2, 0.0f);
		vertices[3] = new Vector3(Size.x/2, Size.y/2, 0.0f);
		vertices[2] = new Vector3(-Size.x/2, Size.y/2, 0.0f);
		
		for(int i = 0; i<4; i++)
		{
			normals[i] = -Vector3.forward;
			tangents[i] = new Vector4(1, 0, 0, 1);
		}
		uvs[1] = new Vector2(Uv.x, Uv.y);
		uvs[0] = new Vector2(Uv.x + UvLength.x, Uv.y);
		uvs[3] = new Vector2(Uv.x + UvLength.x, Uv.y + UvLength.y);
		uvs[2] = new Vector2(Uv.x, Uv.y + UvLength.y);
		
		triangles[0] = 0;
		triangles[1] = 1;
		triangles[2] = 3;
		triangles[3] = 1;
		triangles[4] = 2;
		triangles[5] = 3;
		
		Mesh mesh = new Mesh();
		mesh.vertices=vertices;
		mesh.normals=normals;
		mesh.tangents=tangents;
		mesh.triangles=triangles;
		mesh.uv=uvs;
		return mesh;
	}

}
